//
//  ActullyNameController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/26.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "ActullyNameController.h"
#import "ResultController.h"

@interface ActullyNameController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (nonatomic , strong) UIButton * rightSideBtn;
@property (nonatomic , strong) UIButton * wrongSideBtn;
@property (nonatomic , strong) UIButton * inHandBtn;
@property (nonatomic , strong) UIImagePickerController * picker;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;

@property (nonatomic , assign) NSInteger uploadFace;
@property (nonatomic , assign) NSInteger uploadBack;
@property (nonatomic , assign) NSInteger uploadHand;

@property (nonatomic , strong) NSString * typeString;

@end

@implementation ActullyNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)setNavi
{
    self.title = @"实名认证";
}

-(void)setUI
{
    self.uploadBack = 0;
    self.uploadFace = 0;
    self.uploadHand = 0;
    self.scrollview.contentSize = CGSizeMake(0, 130 * 3 + 15 * 4);
    
    self.rightSideBtn = [[UIButton alloc]init];
    [self.scrollview addSubview:self.self.rightSideBtn];
    [self.rightSideBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scrollview);
        make.top.offset(15);
        make.size.mas_offset(CGSizeMake(200, 130));
    }];
    [self.rightSideBtn setImage:Image(@"box_照片正面") forState:(UIControlStateNormal)];
    [self.rightSideBtn addTarget:self action:@selector(rightSide) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.wrongSideBtn = [[UIButton alloc]init];
    [self.wrongSideBtn setImage:Image(@"box_照片背面") forState:(UIControlStateNormal)];
    
    [self.scrollview addSubview:self.wrongSideBtn];
    [self.wrongSideBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scrollview);
        make.top.equalTo(self.rightSideBtn.mas_bottom).offset(15);
        make.size.mas_offset(CGSizeMake(200, 130));
    }];
    [self.wrongSideBtn addTarget:self action:@selector(wrongSide) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.inHandBtn = [[UIButton alloc]init];
    
    [self.inHandBtn setImage:Image(@"box_手持证件") forState:(UIControlStateNormal)];
    [self.scrollview addSubview:self.inHandBtn];
    [self.inHandBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scrollview);
        make.top.equalTo(self.wrongSideBtn.mas_bottom).offset(15);
        make.size.mas_offset(CGSizeMake(200, 130));
    }];
    [self.inHandBtn addTarget:self action:@selector(inHand) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)rightSide
{
    self.typeString = @"正";
    [self presentViewController:self.picker animated:YES completion:nil];
}

- (void)wrongSide
{
    self.typeString = @"反";
    [self presentViewController:self.picker animated:YES completion:nil];
}

- (void)inHand
{
    self.typeString = @"手持";
    [self presentViewController:self.picker animated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [MBProgressHUD showHUDAddedTo:picker.view animated:YES];
    NSData * data = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage], 0.5);
    NSMutableDictionary * dict = @{@"token":self.userToken,@"user_verify_redis_key":self.verifyKeyString}.mutableCopy;
    if ([self.typeString isEqualToString:@"正"]) {
        [dict setValue:@"face" forKey:@"image_type"];
    }else if ([self.typeString isEqualToString:@"反"])
    {
        [dict setValue:@"back" forKey:@"image_type"];
    }else
    {
        [dict setValue:@"combine" forKey:@"image_type"];
    }
    
    [HTTPTool uploadImageWithURL:[NSString stringWithFormat:@"%@member/upload-id-card",BaseUrl] parameters:dict uploadImage:data success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:picker.view];
        [self dismissViewControllerAnimated:YES completion:nil];
        if (isSuccess) {
            
            if ([self.typeString isEqualToString:@"正"]) {
                self.uploadFace = 1;
                [self.rightSideBtn setImage:info[UIImagePickerControllerOriginalImage] forState:(UIControlStateNormal)];
            }else if ([self.typeString isEqualToString:@"反"])
            {
                self.uploadBack = 1;
                [self.wrongSideBtn setImage:info[UIImagePickerControllerOriginalImage] forState:(UIControlStateNormal)];
            }else
            {
                self.uploadHand = 1;
                [self.inHandBtn setImage:info[UIImagePickerControllerOriginalImage] forState:(UIControlStateNormal)];
            }
        }
    } failure:^(NSError *error) {
        [self dismissViewControllerAnimated:YES completion:nil];
        [MBProgressHUD hideHUDForView:picker.view];
    }];
}
- (IBAction)commitAction:(UIButton *)sender {
    if (self.uploadHand && self.uploadFace && self.uploadFace) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSDictionary * dict = @{@"token":self.userToken,@"user_verify_redis_key":self.verifyKeyString};
        [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/save-verify",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (isSuccess) {
                ResultController * vc = [[ResultController alloc]init];
                vc.imageName = @"icon_提交等待";
                vc.labelText = @"您的实名认证已提交\n我们会尽快处理，请耐心等待…";
                vc.isHiddenBtn = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }else
    {
        [MBProgressHUD showError:@"请上传完整照片"];
    }
}

-(UIImagePickerController *)picker
{
    if (!_picker) {
        _picker = [[UIImagePickerController alloc]init];
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        _picker.delegate = self;
    }
    return _picker;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
